/*
 * $Id: PictureFilesFilenameFilter.java 182 2009-06-04 16:30:30Z meyer@alphatec.de $
 * 
 *
 * Copyright 2009 DeltaDesk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package deltadesk.apps.pictureviewer.io;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.List;

import javax.imageio.ImageIO;

/**
 * PictureFilesFilenameFilter
 * 
 * @author Andreas Meyer
 * @version $Revision: 182 $
 */
public class PictureFilesFilenameFilter implements FilenameFilter {
	
	private final List<String> supportedExtensions;
	
	
	public PictureFilesFilenameFilter() {
		
		supportedExtensions = Arrays.asList(ImageIO.getReaderFormatNames());
	}

	/**
	 * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
	 */
	public boolean accept(final File dir, final String name) {

		return isPictureFile(name);
	}
	
	/**
	 * Determine if provided filename is a valid picture file.
	 * 
	 * @param name
	 * @return
	 */
	private boolean isPictureFile(final String name) {
		
		final int idx = name.lastIndexOf(".");
		if(idx < 0) {
			return false;
		}
		
		final String extension = name.substring(idx + 1);
		
		return supportedExtensions.contains(extension);
	}

}
